package com.cyou.nad.utils.jmx;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author luzhaoman
 */
public class FrameWorkLogHelper implements FrameWorkLogHelperMBean {
	private final static Logger log = Logger.getLogger(FrameWorkLogHelper.class);

	private static Boolean IS_CACHE_LOG_ENABLE = false;	//cache日志
	private static Boolean IS_SHARED_SESSION_LOG_ENABLE = false;	//共享session 日志
	private static Boolean IS_CONTROLLER_PROCESS_LOG_ENABLE=true;	//controller 执行效果日志，包括执行时间，执行失败时url的参数
	
	public static Properties pt = null;
	static {
		pt = new Properties();
		InputStream ist = null;
		try {
			ist = FrameWorkLogHelper.class.getResourceAsStream("/framework_log_config.properties");
			pt.load(ist);
			try {
				String s = pt.getProperty("is_cache_log_enable");
				IS_CACHE_LOG_ENABLE = Boolean.valueOf(s);
				s = pt.getProperty("is_shared_session_log_enable");
				IS_SHARED_SESSION_LOG_ENABLE = Boolean.valueOf(s); 
				s = pt.getProperty("is_controller_process_log_enable");
				IS_CONTROLLER_PROCESS_LOG_ENABLE = Boolean.valueOf(s);
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			try {
				if(null != ist) {
					ist.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

    private static void updateProperties() {
        FileOutputStream fost = null;
        try {
            fost = new FileOutputStream(new File(FrameWorkLogHelper.class.getResource("/framework_log_config.properties").getPath()));
            pt.store(fost, null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fost.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public Boolean getIS_CACHE_LOG_ENABLE() {
		return IS_CACHE_LOG_ENABLE;
	}
    
    public static Boolean IS_CACHE_LOG_ENABLE() {
		return IS_CACHE_LOG_ENABLE;
	}

	public void setIS_CACHE_LOG_ENABLE(Boolean iS_CACHE_LOG_ENABLE) {
		if(null != iS_CACHE_LOG_ENABLE) {
			IS_CACHE_LOG_ENABLE = iS_CACHE_LOG_ENABLE;
	        pt.put("is_cache_log_enable", String.valueOf(iS_CACHE_LOG_ENABLE));
	        updateProperties();
		}
	}

	public Boolean getIS_SHARED_SESSION_LOG_ENABLE() {
		return IS_SHARED_SESSION_LOG_ENABLE;
	}
	
	public static Boolean IS_SHARED_SESSION_LOG_ENABLE() {
		return IS_SHARED_SESSION_LOG_ENABLE;
	}

	public void setIS_SHARED_SESSION_LOG_ENABLE(Boolean iS_SHARED_SESSION_LOG_ENABLE) {
		if(null != iS_SHARED_SESSION_LOG_ENABLE) {
			IS_SHARED_SESSION_LOG_ENABLE = iS_SHARED_SESSION_LOG_ENABLE;
	        pt.put("is_shared_session_log_enable", String.valueOf(iS_SHARED_SESSION_LOG_ENABLE));
	        updateProperties();
		}
	}

	@Override
	public Boolean getIS_CONTROLLER_PROCESS_LOG_ENABLE() {
		return IS_CONTROLLER_PROCESS_LOG_ENABLE;
	}

	public static Boolean IS_CONTROLLER_PROCESS_LOG_ENABLE() {
		return IS_CONTROLLER_PROCESS_LOG_ENABLE;
	}

	@Override
	public void setIS_CONTROLLER_PROCESS_LOG_ENABLE(Boolean param) {
		if(null != IS_CONTROLLER_PROCESS_LOG_ENABLE) {
			IS_CONTROLLER_PROCESS_LOG_ENABLE = param;
	        pt.put("is_controller_process_log_enable", String.valueOf(IS_CONTROLLER_PROCESS_LOG_ENABLE));
	        updateProperties();
		}
	}

}
